package com.bing.jh.controller;

import cn.hutool.core.lang.Assert;
import com.bing.bingPro.common.BaseResult;
import com.bing.bingPro.util.ExcelData;
import com.bing.bingPro.util.WebExcelUtil;
import com.bing.jh.entity.HivestatOverViewEntity;
import com.bing.jh.service.HivestatService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
public class HivestatController {

    @Resource
    private HivestatService hivestatService;

    /**
     * 统计概览-基础统计、汇总统计
     */
    @PostMapping("/dataOverview/list")
    public BaseResult<List<HivestatOverViewEntity>> getBaseCountList(@RequestParam(defaultValue = "") Boolean tag,
                                                                     @RequestParam(defaultValue = "") String dataModel,
                                                                     @RequestParam(defaultValue = "") String externalSystem,
                                                                     @RequestParam(defaultValue = "") String dataTime,
                                                                     @RequestParam(defaultValue = "", required = false) String endTime) {
        Assert.notEmpty(dataModel, "dataModel args is must!");
        String province = "";
        List<String> dataModels = Arrays.asList(dataModel.split(","));
        BaseResult<List<HivestatOverViewEntity>> result = new BaseResult<>();
        if (tag) {
            result.setData(hivestatService.getBaseCountList(externalSystem, dataModels, dataTime.replace("-", ""), province));
        } else {
            result.setData(hivestatService.getSummaryCountList(externalSystem, dataModels, dataTime.replace("-", ""),
                    endTime.replace("-", ""), province));
        }
        return result;
    }


    /**
     * 统计概览-导出
     *
     * @param
     * @return xlsx文件
     */
    @PostMapping("/dataOverview/export")
    public void queryDataOverviewExport(HttpServletResponse response,
                                        @RequestParam(defaultValue = "") Boolean tag,
                                        @RequestParam(defaultValue = "") String dataModel,
                                        @RequestParam(defaultValue = "") String externalSystem,
                                        @RequestParam(defaultValue = "") String dataTime,
                                        @RequestParam(defaultValue = "", required = false) String endTime) throws IOException {
        Assert.notEmpty(dataModel, "dataModel args is must!");
        String province = "";
        List<String> dataModels = Arrays.asList(dataModel.split(","));
        if (tag) {
            List<HivestatOverViewEntity> baseCountList = hivestatService.getBaseCountList(externalSystem, dataModels, dataTime.replace("-", ""),province);
            try {
                ExcelData excelData = new ExcelData(HivestatOverViewEntity.class,
                        baseCountList);
                String fileName = String.format("统计概览-基础统计-%s", dataTime);
                WebExcelUtil.download(response, fileName, excelData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            List<HivestatOverViewEntity> summaryCountList = hivestatService.getSummaryCountList(externalSystem, dataModels, dataTime.replace("-", ""),
                    endTime.replace("-", ""),province);
            try {
                ExcelData excelData = new ExcelData(HivestatOverViewEntity.class,
                        summaryCountList);
                String fileName = String.format("统计概览-汇总统计-%s-%s", dataTime,endTime);
                WebExcelUtil.download(response, fileName, excelData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }



    /**
     * 查询稽核报表
     *
     * @param tag valid empty error
     * @param dataModel
     * @param dataTime
     * @return 稽核报表数据
     */
    @PostMapping("/data/list")
    public BaseResult<List<Map<String, Object>>> queryDataList(@RequestParam(defaultValue = "") String tag,
                                                               @RequestParam(defaultValue = "") String dataModel,
                                                               @RequestParam(defaultValue = "") String externalSystem,
                                                               @RequestParam(defaultValue = "") String dataTime) {
        Assert.notEmpty(tag, "not tag args !");
        Assert.notEmpty(externalSystem, "externalSystem args is must!");
        Assert.notEmpty(dataModel, "not dataModel args !");
        String province = "";
        BaseResult<List<Map<String, Object>>> bro = new BaseResult<>();
        bro.setData(this.hivestatService.queryDataList(tag, externalSystem, dataModel, dataTime.replace("-", ""),province));
        return bro;
    }
}

